/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.emocbc.cbcgateway.httpclient.xml;

import com.thoughtworks.xstream.XStream;

/**
 *
 * @author GiangLT
 */
public class AckParser {

    private XStream xstream;

    public AckParser() {
        xstream = new XStream();
        xstream.alias("BroadcastAck", BroadcastAck.class);

        xstream.useAttributeFor(Status.class, "val");
        xstream.aliasField("val", Status.class, "val");

        xstream.useAttributeFor(AckMessageType.class, "val");
        xstream.aliasField("val", AckMessageType.class, "val");
    }

    public BroadcastAck parseAck(String xmlAck) {
        return (BroadcastAck) xstream.fromXML(xmlAck);
    }

    public String marshallAck(BroadcastAck ack) {
        return xstream.toXML(ack);
    }

    public static void main(String[] args) {
        Status status = new Status("Completed");

        AckMessageType ackMessageType = new AckMessageType();
        ackMessageType.setVal("BroadcastSubmitAck");
        ackMessageType.setCBCTransactionID("45356546");
        ackMessageType.setCBERequestID("1008");
        ackMessageType.setStatus(status);

        BroadcastAck broadcastAck = new BroadcastAck(ackMessageType);
        AckParser xmlParser = new AckParser();

        String xmlStr = xmlParser.marshallAck(broadcastAck);
        System.out.println(xmlStr);

        BroadcastAck ba = xmlParser.parseAck(xmlStr);
        System.out.println(ba.getAckMessageType().getCBCTransactionID());
        System.out.println(ba.getAckMessageType().getCBERequestID());
        System.out.println(ba.getAckMessageType().getVal());
        System.out.println(ba.getAckMessageType().getStatus().getVal());
    }
}
